package com.vp.org.web.servlet;

import com.vp.org.dao.RecordDao;
import com.vp.org.dao.impl.RecordDaoImpl;
import com.vp.org.pojo.Record;
import com.vp.org.web.base.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@SuppressWarnings("FieldCanBeLocal")
@WebServlet("/record")
public class RecordServlet extends BaseServlet {

    private Record record = null;
    private RecordDao recordDao = null;

    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String id = request.getParameter("id");
        record = new Record();
        recordDao = new RecordDaoImpl();

        record.setId(Integer.parseInt(id));
        if (recordDao.deleteRecord(record) > 0) {
            // 删除成功
            // 地址重定向http://localhost:8080//book?method=findByPage&pageIndex=1
            //response.sendRedirect("http://localhost:8080/admin/queryBook.jsp");
            response.sendRedirect("http://localhost:8080/record?method=findByPage&pageIndex5=1");
        } else {
            // 删除失败
            response.sendRedirect("/admin/error.jsp");
        }
    }

    public void findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //接收用户点击的上一页，下一页或者是指定的页数
        int pageIndex5 = Integer.parseInt(request.getParameter("pageIndex5"));

        recordDao = new RecordDaoImpl();

        // 兑换记录分页查询,默认第一页
        request.getSession().setAttribute("records",recordDao.queryPage(pageIndex5));
        // 兑换记录分页总页数
        request.getSession().setAttribute("recordPage",recordDao.recordPage());
        // 兑换记录存储是第几页
        request.getSession().setAttribute("currentPage5",pageIndex5);

        // 地址重定向
        response.sendRedirect("admin/recordQuery.jsp");
    }
}
